package com.tmc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.intair.EthFare;
import com.tmc.bean.intair.EthFlight;

public class IntairFareUtil {

	public static String url = "http://localhost:8080/wangdianbao";

	public static String httpGet(String path, String request) {
		return HttpClientUtil.sendHttpGet(url + path + request, "UTF-8");
	}

	public static CommonResult buildShoppingResponse(String response) {
		if (ObjectUtil.noText(response)) {
			return CommonResult.makeFail("");
		}
		CommonResult cr = JSON.parseObject(response, CommonResult.class);
		if (!cr.isSuccess()){
			return cr;
		}
		if (cr.getObj() == null){
			return CommonResult.makeSuccess("成功");
		}
		List<EthFare> fares = JSON.parseArray(cr.getObj().toString(), EthFare.class);
		return CommonResult.makeSuccess("成功", fares);
	}
	
	public static CommonResult buildBookingResponse(String response) {
		if (ObjectUtil.noText(response)) {
			return CommonResult.makeFail("");
		}
		CommonResult cr = JSON.parseObject(response, CommonResult.class);
		if (!cr.isSuccess()){
			return cr;
		}
		if (cr.getObj() == null){
			return CommonResult.makeSuccess("成功");
		}
		List<EthFare> fares = JSON.parseArray(cr.getObj().toString(), EthFare.class);
		return CommonResult.makeSuccess("成功", fares);
	}

	public static void setExtInfo(List<EthFare> fares) {
		for (EthFare fare : fares){
			setExtInfo(fare);
		}
	}

	public static void setExtInfo(EthFare fare) {
		int index = 1;
		for (EthFlight flight : fare.getFlights()){
			setExtInfo(flight, index++);
		}
	}
	
	public static void setExtInfo(EthFlight flight, int index) {
		flight.setFlightIndexName(index == 2 ? "回程" : "去程");
		flight.setDuration4Show(getDuration(flight.getDuration()));
		try {
			flight.setWeek(getWeek(DateUtil.format(flight.getDepartureDate(), DateUtil.yyyy_MM_dd)));
		} catch (ParseException e) {
		}
	}
	
	public static String getWeek(Date date){   
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");  
        String week = sdf.format(date);  
        return week;  
    } 
	
	private static String getDuration(String duration) {
		return duration.replace(":", "小时") + "分";
	}
	
	public static String buildRequest(Object req) {
		StringBuilder sb = new StringBuilder();
		JSONObject json = JSONObject.parseObject(JSON.toJSONString(req));
		for (Entry<String, Object> entry : json.entrySet()) {
			sb.append(entry.getKey() + "=" + entry.getValue() + "").append("&");
		}
		return "?" + sb.toString().substring(0, sb.length() - 1);
	}

	public static String buildRequest(Object req, String fareKey) {
		StringBuilder sb = new StringBuilder();
		JSONObject json = JSONObject.parseObject(JSON.toJSONString(req));
		for (Entry<String, Object> entry : json.entrySet()) {
			sb.append(entry.getKey() + "=" + entry.getValue() + "").append("&");
		}
		sb.append("fareKey=" + fareKey);
		return "?" + sb.toString();
	}

}
